//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.controller;

import cn.edu.sdjzu.xg.kcsj.domain.Classroom;
import cn.edu.sdjzu.xg.kcsj.service.ClassroomService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/insertClassroom.ctl")
public class InsertClassroomController extends HttpServlet {
    @Override
    protected void doPost (HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        JSONObject respMessage_jsonObj = new JSONObject ();
        String req_jsonStr = JSONUtil.getJSON (req);
        JSONObject req_jsonObj = JSON.parseObject (req_jsonStr);
        Classroom classroom = req_jsonObj.toJavaObject (Classroom.class);
        if (req_jsonObj == null) {
            respMessage_jsonObj.put ("message","no have request");
        }else {
            try {
                if (ClassroomService.getInstance().add(classroom)) {
                    respMessage_jsonObj.put("message", "add yes");
                } else {
                    respMessage_jsonObj.put("message", "add no");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                respMessage_jsonObj.put("message", "sql have exception!");
            }
        }
        resp.getWriter().println(respMessage_jsonObj);
    }
}
